layui.define(['table', 'form', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , admin = layui.admin;


    table.on("sort(LAY-agent-manage)", function (obj) {
        table.reload("LAY-agent-manage", {
            initSort: obj,
            where: {
                field: obj.field, //排序字段   在接口作为参数字段  field order
                order: obj.type, //排序方式   在接口作为参数字段  field order
            },
        });
    });

    //
    table.render({
        elem: '#LAY-agent-manage'
        , url: '/admin/agent/getlist'
        , cols: [[
            {field: 'id', width: 150, title: 'ID', sort: true}
            , {field: 'uid', width: 150, title: '邀请人ID'}
            , {
                field: '', title: '邀请人昵称', width: 200, templet: function (d) {
                    return d.user ? d.user.nick_name : '';
                }
            }
            , {field: 'member_count', title: '累计邀请人数', width: 120}
            , {field: 'withdrawn_diamond', title: '已领取宝石数', width: 140}
            , {field: 'diamond', title: '剩余宝石数', width: 100}
            , {field: 'total_diamond', title: '累计获得宝石数', width: 140}
            , {field: 'invite_code', title: '邀请码', width: 100}
            , {field: 'created_time', title: '创建时间', minWidth: 100}
            , {title: '操作', width: 130, align: 'center', fixed: 'right', toolbar: '#table-agentlist'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-agent-manage)', function (obj) {
        if (obj.event === 'showUser') {
            var index = layer.open({
                type: 2
                , title: '查看邀请好友'
                , content: '/admin/agent/getuser?referer_id=' + obj.data.uid
                , maxmin: true
                , area: ['800px', '700px']
            });
        }
    });

    //
    table.render({
        elem: '#LAY-agent-user-manage'
        , url: '/admin/agent/getuser?referer_id=' + $('#getAgentId').val()
        , cols: [[
            {field: 'uid', title: '好友ID', width: 220}
            , {
                field: '', title: '好友昵称', width: 200, templet: function (d) {
                    return d.user ? d.user.nick_name : '';
                }
            }
            , {field: 'created_time', title: '邀请时间'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });


    /*—————————————————————————————————————————————————— 提现 ————————————————————————————————————————*/

    table.render({
        elem: '#LAY-agent-withdrawals-manage'
        , url: '/admin/agent/getWithdrawList'
        , cols: [[
            {field: 'id', width: 80, title: 'ID', sort: true}
            , {
                field: '', width: 180, title: '用户', templet: function (d) {
                    return d.agent.user.nick_name + "(" + d.agent.user.id + ")";
                }
            }
            , {field: 'cash', width: 110, title: '提现金额/元'}
            , {field: 'alipay_account', width: 180, title: '支付宝账号'}
            , {field: 'alipay_name', width: 100, title: '支付宝姓名'}
            , {field: 'create_time', width: 170, title: '申请时间'}
            , {
                field: '', minWidth: 100, title: '状态', templet: function (d) {
                    if (d.status == 0) {
                        return '<span class="layui-badge layui-bg-red">未处理</span>';
                    } else if (d.status == 1) {
                        return '<span class="layui-badge layui-bg-green">已打款</span>';
                    } else if (d.status == 2) {
                        return '<span class="layui-badge layui-bg-orange">已拒绝</span>'
                    } else if (d.status == 3) {
                        return '<span class="layui-badge layui-bg-black">异常申请</span>'
                    }
                }
            }
            , {field: 'operate_time', width: 170, title: '处理时间'}
            , {field: 'trade_no', minWidth: 200, title: '转账单号'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-agent-withdrawalslist-withdrawals'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-agent-withdrawals-manage)', function (obj) {
        if (obj.event === 'del') {
            layer.confirm('确定删除该申请吗？', function (index) {
                admin.req({
                    url: '/admin/agent/withdrawals_del_post'
                    , data: {"id": obj.data.id}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-agent-withdrawals-manage");
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            if (obj.data.status != 0) {
                layer.msg("该申请已处理完成，无需再次处理", {icon: 1});
                return;
            }
            layer.open({
                type: 2
                , title: '处理提现申请'
                , content: '/admin/agent/withdrawals_edit?id=' + obj.data.id
                , maxmin: true
                , area: ['650px', '400px']
            });
        }
    });

    exports('agent', {})
});